package com.home.andromotic;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ActividadEliminarPerfil extends Activity {

	private AndromoticDbHelper dBHelper;
	private AndromoticProfile profile;
	private Context context;
	private ListView listaPerfiles;
	private ArrayList<AndromoticProfile> andromoticDevices;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_remove_perfil);
		dBHelper = new AndromoticDbHelper(getApplicationContext());
		profile = new AndromoticProfile();

		listaPerfiles = (ListView) findViewById(R.id.remove_perfil_activity_list);
		recargarLista();		
		/* Listener */
		listaPerfiles.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				profile = andromoticDevices.get(position);
				confirmacion();				
			}
		});

	}
	
	private void confirmacion() {

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				this); 
		alertDialogBuilder.setTitle("Atencion"); 
		alertDialogBuilder
				.setMessage("Desea eliminar este dispositivo")
				.setCancelable(false)
				.setPositiveButton("Aceptar",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
					dBHelper.setRemoveProfile(profile);
					   recargarLista();
					}
				  })
				.setNegativeButton("Cancelar",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						dialog.cancel();
					}
				}); 
				AlertDialog alertDialog = alertDialogBuilder.create(); 
				alertDialog.show();
			}
	
	private void recargarLista() {
		String[] values = new String[] { "ValorA", "ValorB","ValorC" };			
		andromoticDevices = dBHelper.getAndromoticProfiles();				
		ArrayList<String> andromoticDevicesNames = new ArrayList<String>();
		for (AndromoticProfile i : andromoticDevices) {
			andromoticDevicesNames.add(i.getNombre());
			}		
		values = (String[]) andromoticDevicesNames.toArray(new String[andromoticDevicesNames.size()]);		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1, values);
		listaPerfiles.setAdapter(adapter);
	}
	
	
}
